package com.topboya.linker.ui.adapter;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.topboya.linker.R;
import com.topboya.linker.model.response.CustomerByOrgIdResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.utils.LogUtils;

import java.util.List;

/**
 * ExpandableListView 适配器
 */
public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {

    private static final String TAG = "MyExpandableListViewAda-";
     private Context mContext;

    /**
     * 每个分组的名字的集合
     */
    private List<OrganizationResopnse.DocDetailInfoList> groupList;

    /**
     * 所有分组的所有子项的 GridView 数据集合
     */
    private  List<List<CustomerByOrgIdResponse.DepartmentList>> itemList;

    private GridView gridView;

    List<String> departmentnamelist;

    public MyExpandableListViewAdapter(Context context, List<OrganizationResopnse.DocDetailInfoList> groupList ,
                                       List<List<CustomerByOrgIdResponse.DepartmentList>> itemList ,List<String> departmentnamelist) {
        mContext = context;
        this.groupList = groupList;
        this.itemList = itemList;
        this.departmentnamelist = departmentnamelist;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        LogUtils.v(TAG + "getChildrenCount:position:"+groupPosition);
        if (null == itemList) return 0;
        return itemList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {

        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return itemList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup
            parent) {
        if (null == convertView) {

            convertView = View.inflate(mContext, R.layout.expandablelist_group_customer, null);
        }
//        ImageView ivGroup = convertView.findViewById(R.id.iv_group);
        TextView tv_hasptial_name =  convertView.findViewById(R.id.tv_hasptial_name);
        TextView tv_hasptial_level =  convertView.findViewById(R.id.tv_hasptial_level);
//        // 如果是展开状态，就显示展开的箭头，否则，显示折叠的箭头
//        if (isExpanded) {
//            ivGroup.setImageResource(R.drawable.test);
//        } else {
//            ivGroup.setImageResource(R.drawable.navg_home);
//        }
//        // 设置分组组名
        tv_hasptial_name.setText(groupList.get(groupPosition).getOrgName());
        tv_hasptial_level.setText(groupPosition+"组");
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {

        if (null == convertView) {

            convertView = View.inflate(mContext, R.layout.expandablelist_item, null);
        }

        // 因为 convertView 的布局就是一个 GridView，
        // 所以可以向下转型为 GridView
        gridView =convertView.findViewById(R.id.gridview);
        TextView tv_group = convertView.findViewById(R.id.tv_group);
        tv_group.setText(departmentnamelist.get(groupPosition));
        LogUtils.v(TAG + "getChildView()");
        // 创建 GridView 适配器
//        MyGridViewAdapter gridViewAdapter = new MyGridViewAdapter(mContext, itemList.get
//                (groupPosition));
//        gridView.setAdapter(gridViewAdapter);
        gridView.setOnItemClickListener((parent1, view, position, id) -> Toast.makeText(mContext, "点击了第" + (groupPosition + 1) + "组，第" +
                (position + 1) + "项", Toast.LENGTH_SHORT).show());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}